package light.mvc.service.sys.impl;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import light.mvc.dao.BaseDaoI;
import light.mvc.pageModel.base.PageFilter;
import light.mvc.model.sys.SendLogBO;
import light.mvc.pageModel.sys.SendLogVO;
import light.mvc.service.sys.SendLogServiceI;

import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;


@Service
public class SendLogServiceImpl implements SendLogServiceI {

   @Autowired
   private BaseDaoI<SendLogBO> SendLogDao;

   @Override
   public void add(SendLogVO r) {
       SendLogBO t = new SendLogBO();
       t.setSendThemeStyle(r.getSendThemeStyle());
       t.setSendDestination(r.getSendDestination());
       t.setSendStatus(r.getSendStatus());
       t.setSendTime(r.getSendTime());
       t.setSendNote(r.getSendNote());
       SendLogDao.save(t);
   }

   @Override
   public void delete(Long id) {
       SendLogBO t = SendLogDao.get(SendLogBO.class, id);
       SendLogDao.delete(t);
   }

   @Override
   public void edit(SendLogVO r) {
       SendLogBO t = SendLogDao.get(SendLogBO.class, r.getId());
       t.setSendThemeStyle(r.getSendThemeStyle());
       t.setSendDestination(r.getSendDestination());
       t.setSendStatus(r.getSendStatus());
       t.setSendTime(r.getSendTime());
       t.setSendNote(r.getSendNote());
       SendLogDao.update(t);
   }

   @Override
   public SendLogVO get(Long id) {
       SendLogBO t = SendLogDao.get(SendLogBO.class, id);
       SendLogVO r = new SendLogVO();
       r.setId(id);
       r.setSendThemeStyle(t.getSendThemeStyle());
       r.setSendDestination(t.getSendDestination());
       r.setSendStatus(t.getSendStatus());
       r.setSendTime(t.getSendTime());
       r.setSendNote(t.getSendNote());
       return r;
   }

   @Override
   public List<SendLogVO> dataGrid(SendLogVO sendLog, PageFilter ph) {
       List<SendLogVO> ul = new ArrayList<SendLogVO>();
       Map<String, Object> params = new HashMap<String, Object>();
       String hql = " from SendLogBO t ";
       List<SendLogBO> l = SendLogDao.find(hql + whereHql(sendLog, params) + orderHql(ph), params, ph.getPage(), ph.getRows());
       for (SendLogBO t : l) {
           SendLogVO u = new SendLogVO();
           BeanUtils.copyProperties(t, u);
           ul.add(u);
       }
       return ul;
   }

   @Override
   public Long count(SendLogVO sendLog, PageFilter ph) {
       Map<String, Object> params = new HashMap<String, Object>();
       String hql = " from SendLogBO t ";
       return SendLogDao.count("select count(*) " + hql + whereHql(sendLog, params), params);
   }

   private String whereHql(SendLogVO sendLog, Map<String, Object> params) {
       String hql = "";
       if (sendLog != null) {
           hql += " where 1=1 ";
           if (sendLog.getId() != null) {
               hql += " and t.name like :name";
               params.put("name", "%%" + sendLog.getId() + "%%");
           }
       }
       return hql;
   }

   private String orderHql(PageFilter ph) {
       String orderString = "";
       if ((ph.getSort() != null) && (ph.getOrder() != null)) {
           orderString = " order by t." + ph.getSort() + " " + ph.getOrder();
       }
       return orderString;
   }

}